home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / WB_FCOPY.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  2KB  |  89 lines

  1. /* +++Date last modified: 28-Jul-1996 */
  2.  
  3. /*
  4. ** by: Walter Bright via Usenet C newsgroup
  5. **
  6. ** modified by: Bob Stout, Ray Gardner, and David Gersic
  7. **
  8. ** There is no point in going to asm to get high speed file copies. Since it
  9. ** is inherently disk-bound, there is no sense (unless tiny code size is
  10. ** the goal). Here's a C version that you'll find is as fast as any asm code
  11. ** for files larger than a few bytes (the trick is to use large disk buffers):
  12. */
  13.  
  14. #include <stdlib.h>
  15. #include <io.h>
  16. #include <fcntl.h>
  17. #include "snipfile.h"
  18.  
  19. #if !defined(__ZTC__) && !defined(__TURBOC__)
  20.  #include <sys\types.h>
  21. #endif
  22.  
  23. #include <sys\stat.h>
  24.  
  25. int file_copy(char *from, char *to)
  26. {
  27.       int fdfrom,fdto;
  28.       int bufsiz;
  29.  
  30.       fdfrom = open(from,O_RDONLY|O_BINARY,0);
  31.       if (fdfrom < 0)
  32.             return Error_;
  33.  
  34.       /* Open R/W by owner, R by everyone else        */
  35.  
  36.       fdto=open(to,O_BINARY|O_CREAT|O_TRUNC|O_RDWR,S_IREAD|S_IWRITE);
  37.       if (fdto >= 0)
  38.       {
  39.             if (Success_ == fdcopy(fdfrom, fdto))
  40.             {
  41.                   close(fdto);
  42.                   close(fdfrom);
  43.                   return Success_;
  44.             }
  45.             else
  46.             {
  47.                   close(fdto);
  48.                   remove(to);             /* delete any partial file  */
  49.             }
  50.       }
  51.       close(fdfrom);
  52.       return Error_;
  53. }
  54.  
  55. int fdcopy(int fdfrom, int fdto)
  56. {
  57.       int bufsiz, retval = Error_;
  58.  
  59.       /* Use the largest buffer we can get    */
  60.  
  61.       for (bufsiz = 0x4000; bufsiz >= 128; bufsiz >>= 1)
  62.       {
  63.             register char *buffer;
  64.  
  65.             buffer = (char *) malloc(bufsiz);
  66.             if (buffer)
  67.             {
  68.                   while (1)
  69.                   {
  70.                         register int n;
  71.  
  72.                         n = read(fdfrom,buffer,bufsiz);
  73.                         if (n == -1)            /* if error             */
  74.                               break;
  75.                         if (n == 0)             /* if end of file       */
  76.                         {
  77.                               retval = Success_;
  78.                               break;
  79.                         }
  80.                         if (n != write(fdto,buffer,(unsigned) n))
  81.                               break;
  82.                   }
  83.                   free(buffer);
  84.                   break;
  85.             }
  86.       }
  87.       return retval;
  88. }
  89.